/**
 * 
 */
package com.fashop.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;

import com.fashop.dao.LocalDao;
import com.fashop.jpa.Local;

/**
 * @author Alan Dávila
 * e-mail ustargab@gmail.com
 * date 14/01/2014 23:06:43
 */
@Service("localDaoImpl")
public class LocalDaoImpl implements LocalDao {

	private final static Logger LOGGER = Logger.getLogger(LocalDaoImpl.class);
	@Resource(name = "sessionFactory")
    private SessionFactory factoria;
	/* (non-Javadoc)
	 * @see com.fashop.dao.LocalDao#get(com.fashop.jpa.Local)
	 */
	public Local get(final Local local) {
		Session sesion = null;
		Local salida = null;
		try {
			sesion = factoria.openSession();
			salida = (Local) sesion.createQuery("from Local where idlocal = ? and empresa_idempresa = ?")
					.setLong(0, local.getIdlocal())
					.setLong(1, local.getEmpresa_idempresa().getIdempresa())
					.uniqueResult();
		} catch (Exception e) {
			LOGGER.error(e.getClass().toString() + " : obteniendo el local " + local.getIdlocal());
			salida = null;
		}
		return salida;
	}

	/* (non-Javadoc)
	 * @see com.fashop.dao.LocalDao#listado(java.lang.Long)
	 */
	@SuppressWarnings("unchecked")
	public List<Local> listado(Long idempresa) {
		Session sesion = null;
		List<Local> salida = null;
		try {
			sesion = factoria.openSession();
			salida = sesion.createQuery("from Local where empresa_idempresa = ?")
					.setLong(0, idempresa)
					.list();
		} catch (Exception e) {
			LOGGER.error(e.getClass().toString() + " : obteniendo el local " + idempresa);
			salida = null;
		}
		return salida;
	}

}
